window.onload = function () {
    var startPoint=[];
    var endPoint=[];

    var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
    //驾车路线
    var  driving = new BMap.DrivingRoute(map, {
        renderOptions: {
            map: map,
            autoViewport: true
        },
        policy: routePolicy[1],   	//三种驾车策略：最少时间0，最短距离1，避开高速2
    });


    // 创建地图实例
    var map = new BMap.Map("container");
    //起始地图坐标
    var point = new BMap.Point(120.196376, 30.162573);
    map.centerAndZoom(point, 20);
    // map.addControl(new BMap.ScaleControl());
    // map.addControl(new BMap.OverviewMapControl());
    // map.addControl(new BMap.MapTypeControl());
    map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
    //单击获取点击的经纬度
    map.addEventListener("click",function(e){
        if(endPoint.length==0&&startPoint.length==0){
            startPoint.push(e.point.lng);
            startPoint.push(e.point.lat);
            alert(1+" "+e.point.lng+" "+e.point.lat);
        }else if(endPoint.length==0&&startPoint.length!=0){
            endPoint.push(e.point.lng);
            endPoint.push(e.point.lat);
            alert(2+" "+e.point.lng+" "+e.point.lat);
            show(startPoint,endPoint,driving);
        }else if(endPoint.length!=0&&startPoint.length!=0){
            startPoint=[];
            endPoint=[];
            alert(3+" ");
        }

    });

    // 创建标注
    var marker = new BMap.Marker(point);
    marker.enableDragging();






    $(function () {
        var myP1 = new BMap.Point(107.690967,36.913285);    //起点
        var myP2 = new BMap.Point(107.924374,36.915690);    //终点
        // driving.search({title: '杭州', point:myP1}, {title: '北京', point:myP2});    //显示一条公交线路
         driving.search(myP1,myP2);    //显示一条公交线路
    })



}

function show(srtarPoint,endPoint,driving) {
    console.log(srtarPoint,endPoint);
    var myP1 = new BMap.Point(srtarPoint[0],srtarPoint[1]);    //起点
    var myP2 = new BMap.Point(endPoint[0],endPoint[1]);    //终点

    driving.search(myP1,myP2);    //显示一条公交线路

}
